﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PicSync.Backend;
using Facebook;
namespace PicSync.Frontend
{
    /// <summary>
    /// This form pops up to allow the user to auth. PicSync with their Facebook account
    /// </summary>
    public partial class FacebookAuth : Form
    {
        public FacebookAuth()
        {
            InitializeComponent();
        }

        private void FacebookAuth_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// This method retrieves the login url to auth.
        /// </summary>
        public void getAccessToken()
        {
            this.Show();
            Connection.FBC = new FacebookClient();
            var loginUrl = Connection.FBC.GetLoginUrl(new { client_id = Connection.APP_ID, redirect_uri = "http://www.friendlyblitzkrieg.com", scope = "publish_actions , user_photos , publish_stream , photo_upload", response_type = "token" });
            webBrowser.Url = loginUrl;
        }

        private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            FacebookOAuthResult oauthResult;
            if (Connection.FBC.TryParseOAuthCallbackUrl(e.Url, out oauthResult))
                {
                // The url is the result of OAuth 2.0 authentication
                if (oauthResult.IsSuccess)
                    {
                    Connection.Access_Token = oauthResult.AccessToken;
                    Connection.FBC = new FacebookClient(Connection.Access_Token);
                    MessageBox.Show(Connection.Access_Token + " is the token!");
                    this.Dispose();
                    }
                 }   

        }
    }
}
